/*
 * cafe-progress.js
 * Created by PanRS on 2017年5月31日 下午8:12:25. 
 * Copyright (C) 2017 NRIET
 * Description: 
 * 进度条模块
 */

;define(function(){
	
	'use strict';
	
	var win = window,
		$ = jQuery;
	var ALL_INSTANCES = [];
	//构造
	var CFProgress = function(setting){
		var opts = {
				type: false,
				hasMsg: true,
				info: '数据正在加载...',
				width: '150px',
				css: {
					height: 30,
					color: '#0b1f44'
				}
		};
		this.configs = $.extend({
			timer: null
		}, opts, setting);
		this.button = null;
		this.Spinner = null;
		this.init();
	}
	 //原生注册
	CFProgress.prototype = {
			init: function(){
				var _this = this;
				_this.button = document.createElement("button");
				_this.button.className = "ladda-button";
				_this.button.setAttribute("data-style", "contract");
				_this.button.innerHTML = _this.configs.info;
				$(_this.button).css({
					width: _this.configs.width
				});
				_this.Spinner = _this.create(_this.button);
			},
			run: function(){
				if(this.Spinner){
					this.Spinner.start();
					this.Spinner.setProgress( 0.1 );
				}
			},
			startAfter: function(d){
				this.Spinner.startAfter(d);
			},
			stop: function(){
				this.Spinner.stop();
			},
			create: function(button){
				var _this = this,
					cfs = this.configs;
				
				var $Wrapper = $('<div>').addClass('cafe-mask');
				
		        if (typeof button === "undefined") {
		            console.warn("Ladda button target must be defined.");
		            return;
		        }
		        if (!button.querySelector(".ladda-label")) {
		            button.innerHTML = '<span class="ladda-label">' + button.innerHTML + "</span>";
		        }
		        
		        function createSpinner(button) {
		        	var height = button.offsetHeight, 
		        		spinnerColor;
		        	var lines = 12, 
		        		radius = cfs.css.height * .2, 
		        		length = radius * .6, 
		        		width = radius < 7 ? 2 : 3;
		        	var opts = {
			        		color: spinnerColor || "#FF0",
			        		lines: lines,
			        		radius: radius,
			        		length: length,
			        		width: width,
			        		zIndex: "auto",
			        		top: "50%",
			        		left: "50%",
			        		className: ""
			        };
		        	var spconfigs = $.extend(opts, cfs.css);
		        	
		        	return new Spinner(spconfigs);
		        }
		        
		        var spinner = createSpinner(button);
		        var spinnerWrapper = document.createElement("span");
		        	spinnerWrapper.className = "ladda-spinner";
		        button.appendChild(spinnerWrapper);
		        
		        var timer;
		        var instance = {
		        	start: function() {
		                button.setAttribute("disabled", "");
		                button.setAttribute("data-loading", "");
		                clearTimeout(timer);
		                spinner.spin(spinnerWrapper);
		                this.setProgress(0);
		                $(win.document.body).append($Wrapper.append($(button)));
		                return this;
		            },
		            startAfter: function(delay) {
		                clearTimeout(timer);
		                timer = setTimeout(function() {
		                    instance.start();
		                }, delay);
		                return this;
		            },
		            stop: function() {
		                button.removeAttribute("disabled");
		                button.removeAttribute("data-loading");
		                clearTimeout(timer);
		                timer = setTimeout(function() {
		                    spinner.stop();
		                }, 1e3);
		                $Wrapper.remove();
		                return this;
		            },
		            toggle: function() {
		                if (this.isLoading()) {
		                    this.stop();
		                } else {
		                    this.start();
		                }
		                return this;
		            },
		            setProgress: function(progress) {
		                progress = Math.max(Math.min(progress, 1), 0);
		                var progressElement = button.querySelector(".ladda-progress");
		                if (progress === 0 && progressElement && progressElement.parentNode) {
		                    progressElement.parentNode.removeChild(progressElement);
		                } else {
		                    if (!progressElement) {
		                        progressElement = document.createElement("div");
		                        progressElement.className = "ladda-progress";
		                        button.appendChild(progressElement);
		                    }
		                    progressElement.style.width = "5px";
		                }
		            },
		            enable: function() {
		                this.stop();
		                return this;
		            },
		            disable: function() {
		                this.stop();
		                button.setAttribute("disabled", "");
		                return this;
		            },
		            isLoading: function() {
		                return button.hasAttribute("data-loading");
		            },
		            getTarget: function() {
		                return button;
		            }
		        };
		        ALL_INSTANCES.push(instance);
		        return instance;
			}
	}

	//返回对象处理
	var cfPros = null;
	return {
		run: function(opts){
			if(!cfPros) cfPros = new CFProgress(opts);
			cfPros.run();
		},
		close: function(){
			cfPros.stop();
		},
		stopAfter: function(dey){
			if(!dey && document.readySatte == "complete") {
				cfPros.stop();
				return;
			}
			setTimeout(function(){
				cfPros.stop();
			}, dey);
		}
	};
});